/**
 * Created by zhaojm on 15/4/23.
 */
game.Platform = game.BaseRole.extend({
    _sprite : null,
    _world:null,
    _body:null,
    _bodyDef : null,
    _fixtureDef:null,
    _className : "Platform",
    ctor:function(p){
        this._super();

        var sprite = new cc.Sprite(res.platform_png);
        this._sprite = sprite;
        var scaleX = 10000;
        this._sprite.setScaleX(scaleX);
        //this._sprite.setColor(new cc.Color(231, 231, 231));

        p.x += sprite.getContentSize().width * scaleX / 2;

        sprite.x = p.x;
        sprite.y = p.y;

        //this._sprite.setAnchorPoint(cc.p(0, 0));


        var b2BodyDef = Box2D.Dynamics.b2BodyDef
            , b2Body = Box2D.Dynamics.b2Body
            , b2FixtureDef = Box2D.Dynamics.b2FixtureDef
            , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
            , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
            , b2Vec2 = Box2D.Common.Math.b2Vec2;

        // 定义一个物体
        var bodyDef = new b2BodyDef();
        bodyDef.type = b2Body.b2_staticBody;
        bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
        bodyDef.userData = this;
        this._bodyDef = bodyDef;


        // 定义一个形状
        var dynamicBox = new b2PolygonShape();
        dynamicBox.SetAsBox(
            this._sprite.getContentSize().width * scaleX / 2 / PTM_RATIO,
            this._sprite.getContentSize().height / 2 / PTM_RATIO
        );// 大小 半长半宽

        // 定义材质
        var fixtureDef = new b2FixtureDef();
        fixtureDef.shape = dynamicBox;
        fixtureDef.density = 0;   // 密度
        fixtureDef.friction = 0.0;  // 摩擦系数
        fixtureDef.restitution = 0;   // 弹性
        this._fixtureDef = fixtureDef;



    },

    getCollideRect:function(){
        var pos = this._sprite.getPosition();
        var contentSize = this._sprite.getContentSize();
        var scaleX = this._sprite.getScaleX();
        return new cc.Rect(pos.x - contentSize.width * scaleX / 2, pos.y - contentSize.height / 2, contentSize.width * scaleX, contentSize.height);
    },


    addToLayer:function(layer, world){
        this._layer = layer;
        this._world = world;
        layer.addChild(this._sprite);
        this._body = this._world.CreateBody(this._bodyDef);
        this._body.CreateFixture(this._fixtureDef);
    },
    removeFromLayer:function(){
        this._sprite.removeFromParent();
        this._world.DestroyBody(this._body);
    },

});